<?php

namespace app\controller;

use app\BaseController;
use app\model\Images;
use app\model\Imgs;
use app\model\Imgsrelation;
use app\model\Sorts;
use app\model\Tagsrelation;
use app\model\Tags;
use think\facade\Db;
use think\facade\Request;


class Index extends BaseController
{
	
	public function index()
	{
		$ism = $this->isMobile();
		if (!$ism) {
//			// 查询状态为1的用户数据 并且每页显示10条数据
//			$list = Db::name('imgs')->where('title', '美女1号')->paginate(1);
//
//			// 渲染模板输出
//			return view('index', [
//				'list' => $list,
//				'title' => '筱食',
//				'email' => 'thinkphp@qq.com'
//			]);
            return view('index');
		} else {
			return Redirect('http://m.xs.com');
		}
	}
	
	public function updata()
	{
		
		$sort = Sorts::select()->toArray();
		$tag = Tags::select()->toArray();
		
		
		return view('updata', [
			'sort' => $sort,
			'tag' => $tag,
		]);
	}


	
	public function t()
	{
		//新获取的tags
		$tags = '性感,妹子，美腿，丝袜,ya,压抑，鸭腿，御姐';
		$tagsToRe =str_replace('，',',',$tags);
		dump($tagsToRe);
		$tagsToReToArr = explode(',',$tagsToRe);
		dump($tagsToReToArr);
		
		//获取数据库中的tags
		$sqlTags = Sorts::find(1)['tagsid'];
		dump($sqlTags);
		$sqlTagsToArr = explode(',',$sqlTags);
		dump($sqlTagsToArr);
		foreach ($tagsToReToArr as $i){
			if (strpos($sqlTags,$i)!== false){
			
			}else{
				$sqlTagsToArr[] = $i;
			}
		}
		dump($sqlTagsToArr);
		
		$sqlTagsToArrToStr = implode(',',$sqlTagsToArr);
		dump($sqlTagsToArrToStr);
		
		return view('t');
	}
	
	//上传图片
	public function upimgs()
	{
		$title = Request::param('name');
		$hasTitle = Images::where('title',$title)->find();
		if ($hasTitle == null){
			$files = request()->file('upfile');
			if ($files == null) {
				$arr = [
					'status' => '2',
					'content' => '没有选择上传的文件',
				];
				echo json_encode($arr);
				exit;
			} else {
				$savename = [];
				foreach ($files as $file) {
					$savename[] = \think\facade\Filesystem::disk('imgs')->putFile(time(), $file, 'uniqid');
				}
				//获取名称、分类。标签
				$title = Request::param('name');
				$sortid = Request::param('sort');
				$tag = Request::param('tag');
				if ($savename[0]) {
					
					//上传成功后写入数据库
					//images
					$data = [
						'title' => $title,
						'sortid' => $sortid,
						'addtime'=>date('y-m-d h:i:s'),
					];
					$images = Images::create($data);
					//获取到上传后的imagesid
					$imagesId = $images->id;
					//循环写入imgs，并获取imgsid(转化为字符串)
					$imgSid = [];
					foreach ($savename as $key => $user) {
						$sqlImg = Imgs::create([
							'imgurl' => $user,
                            'imagesid'=>$imagesId,
						]);
						$imgSid[]  = $sqlImg->id;
					}
					$imgidToStr = implode(',',$imgSid);
					//写入imgs关系表
					Imgsrelation::create([
						'imagesid'=>$imagesId,
						'imgsid'=>$imgidToStr,
					]);
					//将字符串tag转换成数组[]
					$tags = str_replace('，',',',$tag);
					$tagToArr = explode(',',$tags);
					// $tagToArr = ['性感','妹子','美腿','丝袜'];
					
					$res = [];
					foreach ($tagToArr as $tag){
						$tagIs  = Tags::where('tagname',$tag)->find();
						if ($tagIs != null){
							$res[] = $tagIs->id;
						}else{
							$tagAdd = Tags::create([
								'tagname'=>$tag,
								'sortid'=>$sortid,
							]);
							$res[] = $tagAdd->id;
						}
					}
					//最后$res转换成字符串写入tag关系表
					$resToStr = implode(',',$res);
					Tagsrelation::create([
						'imagesid'=>$imagesId,
						'tagid'=>$resToStr,
					]);
					
					//分类表更新tagsid
					// $tagToArr = ['性感','妹子','美腿','丝袜'];
					// 原来的数据有   性感，妹子
					//现获取出原来的数据
					
					$newTagsId = $this->upSort($tags,$sortid);
					Sorts::update(['tagsid' => $newTagsId], ['id' =>$sortid]);
					
					
					$arr = [
						'status' => '1',
						'content' => '上传成功',
					];
					echo json_encode($arr);
					exit;
				} else {
					$arr = [
						'status' => '2',
						'content' => '上传失败请重试',
					];
					echo json_encode($arr);
					exit;
				}
			}
		}else{
			$arr = [
				'status' => '2',
				'content' => '套图名已存在，请修改后再上传',
			];
			echo json_encode($arr);
			exit;
		}
		
	}
	
	//T.htnl
	public function upload()
	{
		// 获取表单上传文件
		$files = request()->file('image');
		$savename = [];
		foreach ($files as $file) {
			$savename[] = \think\facade\Filesystem::disk('imgs')->putFile(time(), $file, 'uniqid');
		}
		// dump($savename);
		$imgid = [];
		foreach ($savename as $key => $user) {
			$sqlImg = Imgs::create([
				'imgurl' => $user,
			]);
			$imgid[]  = $sqlImg->id;
		}
		$imgidToStr = implode(',',$imgid);
		$imgidToStrToArr = explode(',',$imgidToStr);
		dump($imgidToStrToArr);
		
		
	}
	
	
	
	
	public function upSort($tags,$sortId){
		//新获取的tags
		// $tags = '性感,妹子，美腿，丝袜,ya,压抑，鸭腿，御姐';
		$tagsToRe =str_replace('，',',',$tags);
		// dump($tagsToRe);
		$tagsToReToArr = explode(',',$tagsToRe);
		// dump($tagsToReToArr);
		
		//获取数据库中的tags
		$sqlTags = Sorts::find($sortId)['tagsid'];
		// dump($sqlTags);
		$sqlTagsToArr = explode(',',$sqlTags);
		// dump($sqlTagsToArr);
		foreach ($tagsToReToArr as $i){
			if (strpos($sqlTags,$i)!== false){
			
			}else{
				$sqlTagsToArr[] = $i;
			}
		}
		// dump($sqlTagsToArr);
		
		$sqlTagsToArrToStr = implode(',',$sqlTagsToArr);
		return $sqlTagsToArrToStr;
	}
	//检测是否为手机
	public function isMobile()
	{
		// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
		if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
			return true;
		}
		// 如果via信息含有wap则一定是移动设备
		if (isset ($_SERVER['HTTP_VIA'])) {
			// 找不到为flase,否则为true
			return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
		}
		// 脑残法，判断手机发送的客户端标志,兼容性有待提高
		if (isset ($_SERVER['HTTP_USER_AGENT'])) {
			$clientkeywords = array('nokia',
				'sony',
				'ericsson',
				'mot',
				'samsung',
				'htc',
				'sgh',
				'lg',
				'sharp',
				'sie-',
				'philips',
				'panasonic',
				'alcatel',
				'lenovo',
				'iphone',
				'ipod',
				'blackberry',
				'meizu',
				'android',
				'netfront',
				'symbian',
				'ucweb',
				'windowsce',
				'palm',
				'operamini',
				'operamobi',
				'openwave',
				'nexusone',
				'cldc',
				'midp',
				'wap',
				'mobile'
			);
			// 从HTTP_USER_AGENT中查找手机浏览器的关键字
			if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
				return true;
			}
		}
		// 协议法，因为有可能不准确，放到最后判断
		if (isset ($_SERVER['HTTP_ACCEPT'])) {
			// 如果只支持wml并且不支持html那一定是移动设备
			// 如果支持wml和html但是wml在html之前则是移动设备
			if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
				return true;
			}
		}
		return false;
	}
}
